<?php
/**
 * 非Yii函数公共静态类库
 * @since 2011-8-22
 */
class QAuth {
	public static function  qq_login($appid, $scope, $callback) {
		$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
		$login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" 
			. $appid . "&redirect_uri=" . urlencode($callback)
			. "&state=" . $_SESSION['state']
			. "&scope=".$scope;
		return $login_url;
	}
	public static function  do_post($url, $data) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
		curl_setopt($ch, CURLOPT_POST, TRUE); 
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
		curl_setopt($ch, CURLOPT_URL, $url);
		$ret = curl_exec($ch);
	
		curl_close($ch);
		return $ret;
	}

	public static function  get_url_contents($url)
	{
		if (ini_get("allow_url_fopen") == "1")
			return file_get_contents($url);
	
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_URL, $url);
		$result =  curl_exec($ch);
		curl_close($ch);
	
		return $result;
	}
	
	public static function  get_user_info() {
		$get_user_info = "https://graph.qq.com/user/get_user_info?"
			. "access_token=" . $_SESSION['access_token']
			. "&oauth_consumer_key=" . $_SESSION["appid"]
			. "&openid=" . $_SESSION["openid"]
			. "&format=json";
	
		$info = self::get_url_contents($get_user_info);
		$arr = json_decode($info, true);
		return $arr;
	}
}